﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using VSTO = Microsoft.Office.Tools.Word;
//using Microsoft.Office.Tools.Word.Extensions;
using Microsoft.Office.Interop.SmartTag; 

namespace ContentProcessor
{
    public class OCISmartTag : VSTO.SmartTag
    {

        // Declare Actions for this SmartTag
        private VSTO.Action Action1 = new VSTO.Action("Display property value");

        public OCISmartTag(string smartTagType, string caption)
            : base(
                smartTagType,
                caption)
        {
            this.Terms.AddRange(new string[] {"sales", "organization" });
            Actions = new VSTO.Action[] { Action1 };
            
            Action1.Click +=
                new VSTO.ActionClickEventHandler(Action1_Click);
            
        }

        private void Action1_Click(object sender, VSTO.ActionEventArgs e)
        {
            e.Range.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;
        }

        protected override void Recognize(string text,
            ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
        {
            foreach (string term in this.Terms)
            {
                // Search the text for the current smart tag term.
                int index = this.ParagraphText.IndexOf(term, 0);

                while (index >= 0)
                {
                    // Create a smart tag token and a property bag for the 
                    // recognized term.
                    ISmartTagProperties propertyBag =
                        site.GetNewPropertyBag();

                    
                    // Write a new property value.
                    string key = "Key1";
                    propertyBag.Write(key, DateTime.Now.ToString());
                    propertyBag.Write("color", "yellow"); 

                    // Attach the smart tag to the term in the document
                    this.PersistTag(index, term.Length, propertyBag);
                    

                    // Increment the index and then find the next
                    // instance of the smart tag term.
                    index += term.Length;
                    index = ParagraphText.IndexOf(term, index);
                }
            }
        }

        
        
    }


}
